import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.awt.Component;


public class VueRechercheAuteur extends Vue {
	
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JComboBox comboBox;
	private JTextArea textArea;
	private List list_ouv;
	private List list_art;
	
	private Auteur _auteur;
	
	public VueRechercheAuteur(Controleur controleur) {
		super(controleur);
		
		setTitle("Rechercher par auteur");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
		setBounds(100, 100, 450, 519);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setEditable(true);
		comboBox.setBounds(91, 28, 262, 39);
		contentPane.add(comboBox);
		
		JButton btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String val = (String)comboBox.getSelectedItem();
				getControleur().rechDocsAuteur(val);
			}
		});
		btnRechercher.setBounds(91, 97, 117, 25);
		contentPane.add(btnRechercher);
		
		JButton buttonFermer = new JButton("Fermer");
		buttonFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getControleur().fermerVue(VueRechercheAuteur.this);
			}
		});
		buttonFermer.setBounds(236, 97, 117, 25);
		contentPane.add(buttonFermer);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(37, 158, 358, 2);
		contentPane.add(separator);
		
		JButton btnVisualiser = new JButton("Visualiser");
		btnVisualiser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (list_art.getSelectedItem() != null)
				{
					textArea.setText(_auteur.getDocument(list_art.getSelectedItem().toString(), "Article").afficher());
				}
				else if (list_ouv.getSelectedItem()!=null)
				{
					textArea.setText(_auteur.getDocument(list_ouv.getSelectedItem().toString(), "Ouvrage").afficher());
				}
				else
					textArea.setText("");
			}
		});
		btnVisualiser.setBounds(37, 362, 117, 25);
		contentPane.add(btnVisualiser);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(37, 414, 378, 55);
		contentPane.add(scrollPane_1);
		
		textArea = new JTextArea();
		scrollPane_1.setViewportView(textArea);
		textArea.setEditable(false);
		
		JScrollPane scrollPane = new JScrollPane((Component) null);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(38, 218, 183, 113);
		contentPane.add(scrollPane);
		
		list_ouv = new List();
		list_ouv.setEnabled(false);
		list_ouv.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				if ( list_art.getSelectedIndex() != -1 ) {
					list_art.deselect(list_art.getSelectedIndex());
				}
			}
		});
		scrollPane.setViewportView(list_ouv);
		
		JScrollPane scrollPane_2 = new JScrollPane((Component) null);
		scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_2.setBounds(233, 218, 183, 113);
		contentPane.add(scrollPane_2);
		
		list_art = new List();
		list_art.setEnabled(false);
		list_art.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
				if ( list_ouv.getSelectedIndex() != -1 ) {
					list_ouv.deselect(list_ouv.getSelectedIndex());
				}
			}
		});
		scrollPane_2.setViewportView(list_art);
		
		JLabel label = new JLabel("Ouvrages");
		label.setBounds(48, 186, 76, 14);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("Articles");
		label_1.setBounds(256, 193, 76, 14);
		contentPane.add(label_1);
	}
	
	/*alimente la liste des auteurs*/
	public void alimente(HashMap<String, Auteur> auteurs) {
		Iterator<String> it = auteurs.keySet().iterator();
        while(it.hasNext()) 
        {
        	comboBox.addItem(it.next());
        }
	}
	
	public void alimente(HashSet<Document> docs) {
		
		if (docs.isEmpty() || docs==null)
		{
			Message dialog = new Message("Cet auteur n'a aucun document d'enregistré");
			dialog.setVisible(true);
		}
		else
		{
			list_ouv.removeAll(); list_art.removeAll();
			for (Document doc : docs)
			{
				if (doc.getClass().toString().contentEquals("class Article"))
				{
					list_art.setEnabled(true);
					list_art.add(doc.getTitre()); list_art.add("\n");
				}
				else if (doc.getClass().toString().contentEquals("class Ouvrage"))
				{
					list_ouv.setEnabled(true);
					list_ouv.add(doc.getTitre()); list_art.add("\n");
				}				
			}
		}
	}
	
	public void setAuteur(Auteur auteur) {
		_auteur = auteur;
	}
}
